// @ts-nocheck
/**
 * 通用的工具类
 */

import { ElNotification } from 'element-plus'
import {GM_xmlhttpRequest} from "v$v";
import {errMsg} from "./noticeUtil";

enum msgTypes {
    warn = 'warning',
    ok = 'success',
    err = 'error',
    info = 'info',
}

export function needStop(stop: boolean) {
    if (stop){
        errMsg('停止了')
        throw Error('手动停止')
    }
}

export function sleep(time: number){
    const rate = window['examRate'] ? window['examRate'] : 3
    time = rate * time
    return new Promise((resolve)=>{
        setTimeout(()=>{
            resolve('')
        }, time)
    })
}

export function GmPostServerMsg(url:string,body: object){
    return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            url:url,

            method :"POST",
            data: JSON.stringify(body),
            headers:{
                'Content-Type': "application/json"
            },
            onload:function(xhr){
                resolve(xhr.responseText)

            }
        });
    })
}


export function GmGet(url:string,token:string = ''){
    return new Promise((resolve, reject) => {
        let config = {
            url:url,
            method :"GET",
            onload:function(xhr){
                resolve(xhr.responseText)
            }
        }
        if(token){
            config = {
                url:url,
                method :"GET",
                headers: {
                    'Authorization': 'Bearer ' + token
                },
                onload:function(xhr){
                    resolve(xhr.responseText)
                }
            }
        }
        GM_xmlhttpRequest(config);
    })
}

// export function GmGet(url:string){
//     return new Promise((resolve, reject) => {
//         GM_xmlhttpRequest({
//             url:url,
//             method :"GET",
//             onload:function(xhr){
//                 // console.log(xhr)
//                 resolve(xhr.responseText)

//             }
//         });
//     })
// }

export function GmPost(url:string,body:object = {}){
    return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            url:url,
            method :"POST",
            headers:{
                'Content-Type': "application/json"
            },
            data: JSON.stringify(body),
            onload:function(xhr){
                resolve(xhr.responseText)
            }
        });
    })
}

